import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class loginFrame extends JFrame{
	// data controls
	private String name;
	private String password;
	
	private UserManager um = new UserManager();
	// GUI
	private backgroundPanel picPanel;
	private JPanel controlPanel;
	private JLabel userNameLabel;
	private JTextField userNameTextField;
	private JLabel passwordLabel;
	private JPasswordField passwordTextField;
	private JButton signInButton;
	private JButton cancelButton;
	
	// event handlers
	private SignInHandler signInHandler;
	private CancelHandler cancelHandler;
	
	public loginFrame(){
		initData();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(420, 400);
		this.setResizable(false);
		layoutGUI();
	}
	
	// initialize the data
	public void initData(){
		name = "";
		password = "";
	}
	
	public void layoutGUI(){
		this.setTitle("Login");
		picPanel = new backgroundPanel("login.jpg",0,0,400,300);
		picPanel.setBackground(Color.WHITE);
		controlPanel = new JPanel();
		controlPanel.setBackground(Color.WHITE);
		userNameLabel = new JLabel("User Name: ");
		userNameTextField = new JTextField(20);
		passwordLabel = new JLabel("Password: ");
		passwordTextField = new JPasswordField(20);
		signInButton = new JButton("Sign In");
		cancelButton = new JButton("Cancel");
		
		// add handlers
		signInHandler = new SignInHandler();
		cancelHandler = new CancelHandler();
		signInButton.addActionListener(signInHandler);
		cancelButton.addActionListener(cancelHandler);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		GridBagLayout layout = new GridBagLayout();
		controlPanel.setLayout(layout);
		addComponent(userNameLabel,			controlPanel,	0,	0,	1,	1);
		addComponent(userNameTextField,		controlPanel,	1,	0,	4,	1);
		addComponent(passwordLabel,			controlPanel,	0,	2,	1,	1);
		addComponent(passwordTextField,		controlPanel,	1,	2,	4,	1);
		addComponent(signInButton,			controlPanel,	1,	4,	1,	1);
		addComponent(cancelButton,			controlPanel,	3,	4,	1,	1);
		
		this.setLayout(new BorderLayout());
		this.add(picPanel, BorderLayout.CENTER);
		this.add(controlPanel, BorderLayout.SOUTH);
		
		// size and locate the dialog at the center of the screen
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
	}
	
	// method helping GridBagLayout
	public void addComponent(	JComponent jc, Container c, 
			int x, int y, int w, int h){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	public static void main(String[] args){
		loginFrame temp = new loginFrame();
		temp.setVisible(true);
	}
	
	class SignInHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			password = passwordTextField.getText();
			name = userNameTextField.getText();
			boolean valid = false;
			valid = um.login(name, password);
			
			if(!password.isEmpty() && !name.isEmpty() && valid)
			{
				mainFrame mainScreenFrame = new mainFrame(um);
				mainScreenFrame.setVisible(true);
				loginFrame.this.setVisible(false);
			}
			/*if (userNameTextField.getText().equals(defaultName) 
					&& passwordTextField.getText().equals(defaultPassword)){
				
				System.out.println("Login Successfully");
				System.out.println("Input: "+userNameTextField.getText()+"/ "+passwordTextField.getText());
				
				mainFrame temp = new mainFrame();
				signInDialog.this.setVisible(false);
				temp.setVisible(true);
			}
			*/
			else {
				JOptionPane.showMessageDialog(loginFrame.this,
						"Error: Unauthenticate User", "Error Message", 
						JOptionPane.ERROR_MESSAGE);
				
				System.out.println("Login Error");
				System.out.println("Input: "+userNameTextField.getText()+"/ "+passwordTextField.getText());
				
				userNameTextField.setText("");
				passwordTextField.setText("");
				repaint();
			}
			
		}
		
	}
	
	class CancelHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			System.exit(0);
		}
		
	}
	 
}
